# CFDump Clone
# Hacked togeather by Nick Shearer <njs501+railsdump gmail.com>
# Iniitial code thefted from "Scott" <bauer.mail gmail.com> on the ruby list

class Object
        def to_cfdump_html(value = self.to_s);
                return "<span class='RdBug_#{self.class.name}'>#{value}</span>"
        end
end

class String
	def to_cfdump_html
		if self.is_complex_yaml?
	              html = "<table cellspacing=2 cellpadding=3 class='RdBug_YAML'><thead><td colspan=1 class='RdBug_YAMLHeader'>YAML Encoded</td></thead><tr><td>"                
	              html += YAML.load(self).to_cfdump_html
	              html += "</td></tr></table>"
	              super(html)
		else		
			 super(self) 		
		end				
	end
end

class Symbol
        def to_cfdump_html; super(':' + self.to_s) end
end

class NilClass
        def to_cfdump_html; super('nil') end
end

class Hash
        def to_cfdump_html
                html = "<table cellspacing=2 cellpadding=3 class='RdBug_Hash'><thead><td colspan=2 class='RdBug_HashHeader'>#{self.class}</td></thead>"
                self.each_pair do |k, v|
                        html += "<tr><td class='RdBug_HashKey'>#{k.to_cfdump_html}</td><td>#{v.to_cfdump_html}</td></tr>"
                end
                html += "</table>"
                super(html)
        end
end

class Array
        def to_cfdump_html
                html = "<table cellspacing=2 cellpadding=3 class='RdBug_Array'><thead><td colspan=2 class='RdBug_ArrayHeader'>#{self.class}</td></thead>"
                self.size.times do |index|
                        html += "<tr><td class='RdBug_ArrayKey'>#{index}</td><td>#{self[index]}</td></tr>"
                end
                html += "</table>"
                super(html)
        end
end


module CfdumpHelper

  @inited = false

  def cfdump(dump_var)    
    script = ""    
    write_header(script) if not @inited    
    script << dump_var.to_cfdump_html    
    return script    
  end

  private

  def write_header(script)

    script << '<style type="text/css">
  
  table.RdBug_Array,table.RdBug_Object,table.RdBug_Hash,table.RdBug_YAML,table.RdBug_String,table.RdBug_Symbol{font-family:Verdana, Arial, Helvetica, sans-serif; color:#000000;font-size:12px;}

  .RdBug_YAMLHeader, .RdBug_ArrayHeader,.RdBug_ObjectHeader,.RdBug_HashHeader,.RdBug_StringCHeader,.RdBug_SymbolHeader{ font-weight:bold; color:#FFFFFF; }

  /* array */
  table.RdBug_Array { background-color:#006600; }
  table.RdBug_Array td { background-color:#FFFFFF; }
  table.RdBug_Array td.RdBug_ArrayHeader { background-color:#009900; }
  table.RdBug_Array td.RdBug_ArrayKey { background-color:#CCFFCC; }

  /* YAML */

  table.RdBug_YAML { background-color:#000066; }
  table.RdBug_YAML td { background-color:#FFFFFF; }
  table.RdBug_YAML td.RdBug_YAMLHeader { background-color:#000099; }
  table.RdBug_YAML { background-color:#CCCCFF; }
  
  /* resource */
  table.RdBug_Hash { background-color:#660000; }
  table.RdBug_Hash td { background-color:#FFFFFF; }
  table.RdBug_Hash td.RdBug_HashHeader { background-color:#990000; }
  table.RdBug_Hash td.RdBug_HashKey { background-color:#FFCCCC; }

  </style>' 

    @inited = true    
    
  end

end

ActionController::Base.class_eval do
 helper :cfdump
end
